<!-- 门店列表 -->
<template>
  <div>
    <BasicTable @register="registerTable" :rowSelection="rowSelection">
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:plus-outlined" @click="handleAddStore"> 添加门店</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" :dropDownActions="getDropDownAction(record)" />
      </template>
    </BasicTable>
    <StoreDrawer @register="registerDrawer" @success="handleSuccess"/>
    <StoreSetting @register="registerStoreSetting" @success="reload()"/>
  </div>
</template>

<script lang="ts" name="shop-store" setup>
import { ref, computed, unref } from 'vue';
import { BasicTable, TableAction, ActionItem } from '/@/components/Table'
import StoreDrawer from './StoreDrawer.vue';
import StoreSetting from './StoreSetting.vue';
import { Icon } from '/@/components/Icon';
import { useDrawer } from '/@/components/Drawer';
import { useListPage } from '/@/hooks/system/useListPage'
import { useMessage } from '/@/hooks/web/useMessage';
import { columns, searchFormSchema } from './store.data';
import { pageStore, deleteStore, batchDeleteStore } from './store.api';
import { TenantType } from '/@/enums/bizEnum'
import { useUserStore } from '/@/store/modules/user';
// import { usePermission } from '/@/hooks/web/usePermission'
// const { hasPermission } = usePermission();

const { createMessage, createConfirm } = useMessage();

const [registerDrawer, { openDrawer }] = useDrawer();
const [registerStoreSetting, { openDrawer: openStoreSetting }] = useDrawer();
const userStore = useUserStore()
const selectTenant = userStore.getSelectTenant
const { prefixCls, tableContext } = useListPage({
  designScope: 'store-list',
  tableProps: {
    title: '门店列表',
    api: pageStore,
    bordered: false,
    columns: columns,
    size: 'small',
    formConfig: {
      showResetButton: false,
      schemas: searchFormSchema,
    },
    actionColumn: {
      width: 120,
    },
    beforeFetch: (params) => {
      return Object.assign({ 'column': 'createdAt', 'order': "desc" }, params)
    },
  }
})

const [registerTable, { reload, updateTableDataRecord }, { rowSelection, selectedRowKeys }] = tableContext

function handleAddStore() {
  openDrawer(true, {
    isTenantMember: false
  })
}

async function handleEdit(record: Recordable) {
  openDrawer(true, {
    record,
    isUpdate: true,
    showFooter: true,
  });
}

async function handleDelete(record) {
  await deleteStore({ id: record.id }, reload);
}

async function batchHandleDelete() {
  await batchDeleteStore({ ids: selectedRowKeys.value }, () => {
    selectedRowKeys.value = []
    reload()
  })
}

function handleSuccess({ values }) {
  reload()
  updateTableDataRecord(values.id, values);
  createMessage.success("编辑成功")
}

function getTableAction(record): ActionItem[] {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record)
    },
    {
      label: '设置',
      onClick: handleSettingStore.bind(null, record)
    },
    
    {
      label: '删除',
      ifShow: () => selectTenant.tenantType !== TenantType.SUB,
      popConfirm: {
        title: `是否确认删除 ${record.name}`,
        confirm: handleDelete.bind(null, record)
      },
    },
  ]
}

const handleSettingStore = (record) => {
  openStoreSetting(true, { record })
}



function getDropDownAction(record): ActionItem[] {
  return [
    
  ]
}
</script>

<style scoped></style>
